package corbaServer;

import java.util.Iterator;
import java.util.List;
import corbaServer.corba.ICorbaServerPOA;
import corbaServer.RaceDO;
import corbaServer.corba.RaceDTO;
import corbaServer.dao.CorbaRaceServerDAO;
import java.sql.SQLException;

public class CorbaServer extends ICorbaServerPOA
{
	String dbName;
	public CorbaServer(String dbName)
	{
		this.dbName = dbName;
	}
	
	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public RaceDTO[] getRaces() 
	{
		CorbaRaceServerDAO corbaRaceServerDAO = new CorbaRaceServerDAO();
		List<RaceDO> racesDO = null;
		RaceDTO racesDTO[] = null;
		RaceDO r = null;
		
		try
		{
			corbaRaceServerDAO.connect(dbName);
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		try
		{
			racesDO = corbaRaceServerDAO.getRaces();
			racesDTO = new RaceDTO[racesDO.size()];			
			Iterator iter = racesDO.iterator();
			int i = 0;
			while(iter.hasNext())
			{
				r = (RaceDO)iter.next();
				racesDTO[i] = new RaceDTO(r.getName(), r.getDistance(), r.getDate(), r.getPlaces());
				i++;
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}	
		try
		{
			corbaRaceServerDAO.disconnect();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	return racesDTO;
	}
	
}
